<?php
/**
 * WpFileLog.php
 * 
 * This file contains the WpFileLog class which provides logic to write log entries to the file system.
 * The filepath must be provided to the constructor
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 */

/**
 * The WpLog interface to implement
 */
require_once(dirname(__FILE__).'/IWpLog.php');

/**
 * The abstract class WpLogBase to extend
 */
require_once(dirname(__FILE__).'/WpLogBase.php');

/**
 * A class to write log entries to the file system
 * 
 * Example:
 * <code>
 * $l = new WpFileLog('/home/user/log/logfile.txt');
 * $l->logError('location1', 'user', 'a title', 'An error message', '0', 1);
 * $l->logAction('location2', 'user', 'a title', 'Done something', '0', 1);
 * $l->logAudit('location3', 'user', 'a title', 'Something important has been changed', '0');
 * 
 * //Content of logfile.txt would be:
 * 2010-10-10[tab]1[tab]error[tab]0[tab]a title[tab]An error message[tab]user[tab]location1
 * 2010-10-10[tab]1[tab]action[tab]0[tab]a title[tab]Done something[tab]user[tab]location2
 * 2010-10-10[tab]1[tab]audit[tab]0[tab]a title[tab]Something important has been changed[tab]user[tab]location3
 * 
 * </code>
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 */
class WpFileLog extends WpLogBase implements IWpLog
{
	/**
   * The log filepath
   */
	private $_filepath = '';
  
  /**
   * Determines wether to write in realtime or not
   */
  private $_realtime = false;
  
  /**
   * The log cache
   */
  private $_cache = array();
	
	/** 
	 * The class constructor
	 */
	public function __construct($filepath, $realtime=false)
	{
		parent::__construct();
		$this->_filepath = $filepath;
		$this->_realtime = $realtime;
    
		if (!file_exists(dirname($filepath))) {
			die('Fatal error: Directory '.dirname($filepath).' does not exists. Log file could not be written.');
		}
    
    //-- Check if writeable --
    if (!is_writeable(dirname($filepath))) {
      die('Fatal error: Directory '.dirname($filepath).' is not writeable.');
    }
	}
  
  /**
   * The class destructor
   */
  public function __destruct()
  {
    $loc = basename(__FILE__).'/'.__METHOD__;
    if (count($this->_cache) < 1) {
      return;
    }
    
    if (!file_exists($this->_filepath)) {
      file_put_contents($this->_filepath, implode("\n", $this->_cache));
    } else {
      file_put_contents($this->_filepath, implode("\n", $this->_cache), FILE_APPEND);
    }
    
  }
	
	/**
	 * Logs an entry to the file system
	 * @param object $logObject The log entry to persistantly save
	 * @return bool True if successfully saved, false else
	 */
	protected function _log2x($logObject)
	{
		if (!array_key_exists('level', $logObject)) {
			$level = 1;
		} else {
			$level = $logObject['level'];
		}
		
		$strData = $logObject['date']."\t".strval($level)."\t".$logObject['type']."\t".
			$logObject['number']."\t".$logObject['title']."\t".$logObject['body']."\t".
			$logObject['user']."\t".$logObject['location'];
		
    if ($this->_realtime === false) {
      $this->_cache[] = $strData;
      return;
    }
    
		if (!file_exists($this->_filepath)) {
		  file_put_contents($this->_filepath, $strData);
		} else {
		  file_put_contents($this->_filepath, "\n".$strData, FILE_APPEND);
		}
	}
}

/* End of file WpFileLog.php */
/* Location: ./wildpack/base/wplog/WpFileLog.php */ 
